﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Xml;
using ArkEC.CoreCom.CacheStore;
using ArkEC.CoreCom.Runtime;

namespace ArkEC.CoreCom.Datastruct.WebHandler
{
    /// <summary>
    /// Ajax请求的存储区
    /// </summary>
    public class BizActions : CacheType
    {
        /// <summary>
        /// 
        /// </summary>
        private static string _CacheNameField = "BizActions";

        #region 构造函数

        /// <summary>
        /// 唯一实例
        /// </summary>
        private static BizActions mInstance = new BizActions();
        public static BizActions Instance
        {
            get 
            {
                return mInstance; 
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        private BizActions() 
        {
            this.CacheName = _CacheNameField;
        }

        #endregion

        /// <summary>
        /// 读取缓存数据
        /// </summary>
        /// <returns></returns>
        public override Dictionary<string, object> ReadCache()
        {
            Assembly bizAssembly = null;
            Assembly bizObjAssembly = null;
            Dictionary<string, object> Methods = new Dictionary<string, object>();

            string url = AppDomain.CurrentDomain.BaseDirectory + RunSetting.ActionMappingFilePath;
            using (XmlReader menuReader = XmlReader.Create(url))
            {
                while (menuReader.Read())
                {
                    if (menuReader.NodeType == XmlNodeType.Element)
                    {
                        if (menuReader.Name == "Assembly")
                        {
                            bizAssembly = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + menuReader["TypeAssembly"]);
                            bizObjAssembly = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + menuReader["ParamTypeAssembly"]);
                        }
                        else if (menuReader.Name == "ActionMapping")
                        {
                            Item item = new Item();

                            string key = menuReader["Key"];
                            string type = menuReader["Type"];
                            string method = menuReader["Method"];
                            string paramType = menuReader["ParamType"];
                            item.BizType = bizAssembly.GetType(type);
                            item.Method = item.BizType.GetMethod(method);

                            if (!string.IsNullOrEmpty(paramType))
                            {
                                item.ParamType = bizObjAssembly.GetType(paramType);
                            }

                            Methods.Add(key, item);
                        }
                    }
                }
            }

            return Methods;
        }

        /// <summary>
        /// Ajax请求映射的方法和参数
        /// </summary>
        public class Item
        {
            /// <summary>
            /// 
            /// </summary>
            public Type BizType;

            /// <summary>
            /// 执行方法
            /// </summary>
            public MethodInfo Method;

            /// <summary>
            /// 参数类型
            /// </summary>
            public Type ParamType;
        }
    }
}
